﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMSalesManagement.Manage.FrontPageManagement
{
    public partial class DetailMessage : System.Web.UI.Page
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            lnkbtnDelete.Attributes.Add("OnClick", "javascript:return confirm('确定删除该访客留言吗?')");
            this.lblReply.Text = "*";
            this.lblResults.Text = "";
            if (!IsPostBack)
            {
                if (Session["messageId"] != null)
                {
                    int idMessage = Convert.ToInt32(Session["messageId"]);
                    var result =CRM.Business.Messages.GetMessageById(idMessage);
                    if (result != null)
                    {
                        //this.lblMessageId.Text = result.messageId.ToString();
                        this.lblVisitorName.Text = result.name.ToString();
                        this.lblVisitorContact.Text = result.contact.ToString();
                        this.divContentText.InnerHtml = result.messageText.ToString();
                        this.lblVisiteTime.Text = result.visitTime.ToString();
                        this.lblReplyTime.Text = result.replyTime.ToString();
                        this.divReplyMessage.InnerHtml = result.reply;
                        //显示如下：
                        //IsReplying(false);
                        if (result.isPublish == true)
                        {
                            this.lnkbtnPost.Text = "重新审核";
                            this.lblIsPublish.Text = "已审核通过";
                        }
                        else
                        {
                            this.lnkbtnPost.Text = "审核通过";
                            this.lblIsPublish.Text = "等待审核";
                        }
                        if (this.divReplyMessage.InnerText.Trim() == "")
                        {
                            this.replylink.InnerText = "回复";
                           // IsReply(false);
                        }
                        else
                        {
                            this.replylink.InnerText = "重新回复";
                           // IsReply(true);
                        }
                    }
                }
            }

        }

        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {
            if (Session["messageId"] != null)
            {
                var messbox = CRM.Business.Messages.GetMessageById(int.Parse(Session["messageId"].ToString()));
                if (messbox != null)
                {
                    bool delete =CRM.Business.Messages.DeleteVisitorMessage(messbox);
                    if (delete)
                    {
                        this.lblResults.Text = "删除成功";
                        Response.Redirect("MainMessage.aspx");
                    }
                    else
                    {
                        this.lblResults.Text = "删除失败";
                    }
                }
            }
        }
        //发布
        protected void lnkbtnPost_Click(object sender, EventArgs e)
        {
            if (Session["messageId"] != null)
            {
                var postmess = CRM.Business.Messages.GetMessageById(int.Parse(Session["messageId"].ToString()));
                if (postmess != null)
                {
                    if (this.lnkbtnPost.Text == "审核通过")
                    {
                        postmess.isPublish = true;
                        if (CRM.Business.Messages.PostVisitorMessage(postmess))
                        {
                            this.lblResults.Text = "审核成功";
                            this.lnkbtnPost.Text = "重新审核";
                            this.lblIsPublish.Text = "已审核通过";
                        }
                        else
                        {
                            this.lblResults.Text = "审核失败";
                        }
                    }
                    else
                    {
                        postmess.isPublish = false;
                        if (CRM.Business.Messages.PostVisitorMessage(postmess))
                        {
                            this.lblResults.Text = "重新审核成功";
                            this.lnkbtnPost.Text = "审核通过";
                            this.lblIsPublish.Text = "等待审核";
                        }
                        else
                        {
                            this.lblResults.Text = "重新审核失败";
                        }
                    }
                }
            }
        }
        //回复提交
        protected void btnPutIn_Click(object sender, EventArgs e)
        {
            this.lblReply.Text = "*";
            if (this.txtReply.Text.Trim() == "")
            {
                this.lblReply.Text = "*回复内容不能为空";
                txtReply.Focus();
            }
            else
            {
                if (Session["messageId"] != null)
                {
                    var replyMessage = CRM.Business.Messages.GetMessageById(int.Parse(Session["messageId"].ToString()));
                    if (replyMessage != null)
                    {
                        replyMessage.replyTime = DateTime.Now;
                        replyMessage.reply = this.txtReply.Text;
                        bool reply =CRM.Business.Messages.ReplyVisitorMessage(replyMessage);
                        if (reply)
                        {
                            //this.lblResults.Text = "提交成功";
                            this.lblReplyTime.Text = DateTime.Now.ToString();
                            this.divReplyMessage.InnerHtml = this.txtReply.Text;
                            this.txtReply.Text = "";

                            this.replylink.InnerText = "重新回复";
                           // IsReplying(false);
                            //IsReply(true);
                        }
                        else
                        {
                            this.lblResults.Text = "提交失败";
                        }
                    }
                }
            }
        }

        //重置
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtReply.Text = "";
            this.txtReply.Focus();
            this.lblReply.Text = "*";
            this.lblResults.Text = "";
        }
    }
}